export * from './modules/copyright';
export * from './modules/artist';
export * from './modules/album';
export * from './modules/song';
export * from './modules/base';
export * from './modules/strategy';
export * from './modules/lyrics';
export * from './modules/simigroup';
export * from './modules/user';

export type KeyObj = Record<string, any>;
export type MapObj = Map<string | number, string>;
/**
 * object的对象转为key value 的list
 * @param _obj
 */
export function objToList(_obj: KeyObj | MapObj, formatKey?: (key: string) => string | number) {
  if (_obj instanceof Map) {
    return [..._obj].map((item) => ({
      value: formatKey ? formatKey(item[0].toString()) : item[0],
      text: item[1],
    }));
  }
  return Object.keys(_obj).map((key: string) => {
    return {
      value: formatKey ? formatKey(key) : key,
      text: _obj[key],
    };
  });
}


export function objToListWithKey(_obj: KeyObj, formatKey?: (key: string) => string | number) {
  return Object.keys(_obj).map((key: string) => {
    return {
      value: formatKey ? formatKey(key) : key,
      text: `${key}. ${_obj[key]}`,
    };
  });
}

export function objToValueLabelList(_obj: KeyObj, formatKey?: (key: string) => string | number) {
  return Object.keys(_obj).map((key: string) => {
    return {
      value: formatKey ? formatKey(key) : key,
      label: _obj[key],
    };
  });
}

export function objToValueLabelListWithKey(
  _obj: KeyObj,
  formatKey?: (key: string) => string | number,
) {
  return Object.keys(_obj).map((key: string) => {
    return {
      value: formatKey ? formatKey(key) : key,
      label: `${key}. ${_obj[key]}`,
    };
  });
}

type EnumToArrayItem = {
  value: number;
  label: string;
};
export type EnumToArrayParams = Record<string, EnumToArrayItem>;

export const enumToArray = (_obj: EnumToArrayParams) => {
  return Object.keys(_obj).map((key) => {
    return _obj[key];
  });
};

// 枚举类转出的key value列表转key value对象
export const enumArrayTrans = (_array: EnumToArrayItem[]) => {
  const returnObj = {};
  _array.map((item) => {
    returnObj[item.value] = item.label;
    return item;
  });
  return returnObj;
};

export type ValueTextType = {
  value: string | number;
  text: string;
};

export type IdNameType = {
  id: string | number;
  name: string;
  [key: string]: any;
};

// 获取value对应对象的text值
export const getText = (value: string | number, list: ValueTextType[]) => {
  return list.find((item) => item.value === value)?.text;
};

export type ValueLabelType = {
  value: string | number;
  label?: string;
  text?: string;
  [key: string]: any;
};

// 获取value对应对象的label值
export const getLabel = (value: string | number, list: ValueLabelType[]) => {
  return list.find((item) => item.value === value)?.label;
};

export const getEnhancedOptions = (list: ValueLabelType[]) => {
  return list.map((item) => {
    const key = item.label ? 'label' : 'text';
    return {
      ...item,
      [key]: `${item.value}. ${item[key]}`,
    };
  });
};

export const labelToText = (list: ValueLabelType[]): ValueTextType[] => {
  return list.map((item) => ({
    ...item,
    text: item.label || '',
  }));
};

export const textToLabel = (list: ValueTextType[]): ValueLabelType[] => {
  return list.map((item) => ({
    ...item,
    label: item.text,
  }));
};

export const textToLabelWithKey = (list: ValueTextType[]): ValueLabelType[] => {
  return list.map((item) => ({
    ...item,
    label: `${item.value}. ${item.text}`,
  }));
};

export const idNameToLabel = (list: IdNameType[]): ValueLabelType[] => {
  return list.map(({ id, name, ...rest }) => ({
    ...rest,
    value: id,
    label: name,
  }));
};

export const idNameToLabelWithKey = (list: IdNameType[]): ValueLabelType[] => {
  return list.map(({ id, name, ...rest }) => ({
    ...rest,
    value: id,
    label: `${id}. ${name}`,
  }));
};

// 获取value对应对象的name值
export const getNameFromIdName = (value: string | number, list: IdNameType[]) => {
  return list.find((item) => item.id === value)?.name;
};
